parameters:
  osGroup: ''
  osSubgroup: ''
  archType: ''
  buildConfig: ''
  runtimeFlavor: ''
  helixQueues: ''
  targetRid: ''
  nameSuffix: ''
  platform: ''
  shouldContinueOnError: ''
  rootFolder: ''
  includeRootFolder: ''
  displayName: ''
  artifactName: ''
  archiveExtension: ''
  archiveType: ''
  tarCompression: ''


steps:
  # Uncomment to reenable package replacement
  #- task: DownloadPipelineArtifact@2
  #  displayName: Download runtime packages
  #  inputs:
  #    artifact: 'IntermediateArtifacts'
  #    path: $(Build.SourcesDirectory)/MauiTesting/ArtifactPacks
  #    patterns: |
  #      IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NETCore.App.Runtime.Mono.android-!(*.symbols).nupkg
  #      IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NETCore.App.Runtime.Mono.ios-!(*.symbols).nupkg
  #      IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NETCore.App.Runtime.Mono.iossimulator-!(*.symbols).nupkg
  #      IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NETCore.App.Runtime.Mono.maccatalyst-!(*.symbols).nupkg

  #    # Other artifacts to include once they are being built
  #    # EX. IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NETCore.App.Runtime.Mono.maccatalyst-*.nupkg

  #- task: CopyFiles@2
  #  displayName: Flatten packages
  #  inputs:
  #    sourceFolder: $(Build.SourcesDirectory)/MauiTesting/ArtifactPacks
  #    contents: '*/Shipping/*.nupkg'
  #    cleanTargetFolder: false
  #    targetFolder: $(Build.SourcesDirectory)/MauiTesting/ArtifactPacks
  #    flattenFolders: true

  #- script: |
  #    for file in *.nupkg
  #      do
  #        mv -v "$file" "${file%.nupkg}.zip"
  #      done
  #  displayName: Change nupkgs to zips
  #  workingDirectory: $(Build.SourcesDirectory)/MauiTesting/ArtifactPacks


  ##Unzip the nuget packages to make the actual runtimes accessible
  #- task: ExtractFiles@1
  #  displayName: Extract android-arm runtime
  #  inputs:
  #      archiveFilePatterns: $(Build.SourcesDirectory)/MauiTesting/ArtifactPacks/Microsoft.NETCore.App.Runtime.Mono.android-arm.*.zip
  #      destinationFolder: $(Build.SourcesDirectory)/MauiTesting/ArtifactPacks/Microsoft.NETCore.App.Runtime.Mono.android-arm
  #      overwriteExistingFiles: true
  #      cleanDestinationFolder: false
  #- task: ExtractFiles@1
  #  displayName: Extract android-arm64 runtime
  #  inputs:
  #      archiveFilePatterns: $(Build.SourcesDirectory)/MauiTesting/ArtifactPacks/Microsoft.NETCore.App.Runtime.Mono.android-arm64.*.zip
  #      destinationFolder: $(Build.SourcesDirectory)/MauiTesting/ArtifactPacks/Microsoft.NETCore.App.Runtime.Mono.android-arm64
  #      overwriteExistingFiles: true
  #      cleanDestinationFolder: false
  #- task: ExtractFiles@1
  #  displayName: Extract android-x86 runtime
  #  inputs:
  #      archiveFilePatterns: $(Build.SourcesDirectory)/MauiTesting/ArtifactPacks/Microsoft.NETCore.App.Runtime.Mono.android-x86.*.zip
  #      destinationFolder: $(Build.SourcesDirectory)/MauiTesting/ArtifactPacks/Microsoft.NETCore.App.Runtime.Mono.android-x86
  #      overwriteExistingFiles: true
  #      cleanDestinationFolder: false
  #- task: ExtractFiles@1
  #  displayName: Extract android-x64 runtime
  #  inputs:
  #      archiveFilePatterns: $(Build.SourcesDirectory)/MauiTesting/ArtifactPacks/Microsoft.NETCore.App.Runtime.Mono.android-x64.*.zip
  #      destinationFolder: $(Build.SourcesDirectory)/MauiTesting/ArtifactPacks/Microsoft.NETCore.App.Runtime.Mono.android-x64
  #      overwriteExistingFiles: true
  #      cleanDestinationFolder: false
  #- task: ExtractFiles@1
  #  displayName: Extract ios-arm64 runtime
  #  inputs:
  #      archiveFilePatterns: $(Build.SourcesDirectory)/MauiTesting/ArtifactPacks/Microsoft.NETCore.App.Runtime.Mono.ios-arm64.*.zip
  #      destinationFolder: $(Build.SourcesDirectory)/MauiTesting/ArtifactPacks/Microsoft.NETCore.App.Runtime.Mono.ios-arm64
  #      overwriteExistingFiles: true
  #      cleanDestinationFolder: false
  #- task: ExtractFiles@1
  #  displayName: Extract maccatalyst-x64 runtime
  #  inputs:
  #      archiveFilePatterns: $(Build.SourcesDirectory)/MauiTesting/ArtifactPacks/Microsoft.NETCore.App.Runtime.Mono.maccatalyst-x64.*.zip
  #      destinationFolder: $(Build.SourcesDirectory)/MauiTesting/ArtifactPacks/Microsoft.NETCore.App.Runtime.Mono.maccatalyst-x64
  #      overwriteExistingFiles: true
  #      cleanDestinationFolder: false
  #- task: ExtractFiles@1
  #  displayName: Extract iossimulator-x64 runtime
  #  inputs:
  #      archiveFilePatterns: $(Build.SourcesDirectory)/MauiTesting/ArtifactPacks/Microsoft.NETCore.App.Runtime.Mono.iossimulator-x64.*.zip
  #      destinationFolder: $(Build.SourcesDirectory)/MauiTesting/ArtifactPacks/Microsoft.NETCore.App.Runtime.Mono.iossimulator-x64
  #      overwriteExistingFiles: true
  #      cleanDestinationFolder: false

  # Get the current maui nuget config so all things can be found and darc based package sources are kept up to date.
  - script: |
      echo '{}' > ./global.json 
    displayName: Create global.json
    workingDirectory: $(Build.SourcesDirectory)
  
  - script: |
      curl -o NuGet.config 'https://raw.githubusercontent.com/dotnet/maui/net7.0/NuGet.config'
      curl -o dotnet-install.sh 'https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh'
      curl -Lo maui-supported-sdk-version.json 'https://aka.ms/dotnet/sdk/maui/net7.0.json'
      version=$(sed -nr 's/\s*"version": "(.*)"/\1/p' ./maui-supported-sdk-version.json)
      chmod -R a+rx .
      ./dotnet-install.sh --version $version --install-dir .
      ./dotnet --info
      ./dotnet workload install maui --from-rollback-file https://aka.ms/dotnet/maui/net7.0.json --configfile NuGet.config
    displayName: Install MAUI workload
    workingDirectory: $(Build.SourcesDirectory)

  - script: $(Build.SourcesDirectory)/eng/testing/performance/create-provisioning-profile.sh
    displayName: Create iOS code signing and provisioning profile

  - script: |
      ./dotnet new maui -n MauiTesting
      cd MauiTesting
      cp $(Build.SourcesDirectory)/src/tests/Common/maui/MauiScenario.props ./Directory.Build.props
      cp $(Build.SourcesDirectory)/src/tests/Common/maui/MauiScenario.targets ./Directory.Build.targets
      cp $(Build.SourcesDirectory)/NuGet.config ./NuGet.config
    displayName: Setup MAUI Project
    workingDirectory: $(Build.SourcesDirectory)

  - script: |
      chmod -R a+r .
      # Restore is split out because of https://github.com/dotnet/sdk/issues/21877, can be removed with --no-restore once fixed
      ../dotnet restore
      ../dotnet publish -bl:MauiAndroid.binlog -f net7.0-android -c Release -r android-arm64 --no-restore --self-contained
      mv ./bin/Release/net7.0-android/android-arm64/com.companyname.mauitesting-Signed.apk ./MauiAndroidDefault.apk
    displayName: Build MAUI Android
    workingDirectory: $(Build.SourcesDirectory)/MauiTesting

    # This step pulls the product version from the used Microsoft.Maui.dll file properties and saves it for upload with the maui test counter.
    # We pull from this file as we did not find another place to reliably get the version information pre or post build.
  - powershell: |
      $RetrievedMauiVersion = Get-ChildItem .\obj\Release\net7.0-android\android-arm64\linked\Microsoft.Maui.dll | Select-Object -ExpandProperty VersionInfo | Select-Object ProductVersion | Select-Object -ExpandProperty ProductVersion
      $RetrievedMauiVersion
      Write-Host "##vso[task.setvariable variable=mauiVersion;isOutput=true]$RetrievedMauiVersion"
    name: getMauiVersion
    displayName: Get and Save MAUI Version
    workingDirectory: $(Build.SourcesDirectory)/MauiTesting

  - script: |
      set -x
      pwd
      git clone https://github.com/microsoft/dotnet-podcasts.git -b net7.0 --single-branch
      cd dotnet-podcasts
      cp $(Build.SourcesDirectory)/src/tests/Common/maui/MauiScenario.props ./Directory.Build.props
      cp $(Build.SourcesDirectory)/src/tests/Common/maui/MauiScenario.targets ./Directory.Build.targets
    displayName:  Clone podcast app
    workingDirectory: $(Build.SourcesDirectory)

  - script: |
      chmod -R a+r .
      # remove net7.0-maccatalyst to work around https://github.com/dotnet/sdk/issues/21877
      cp MauiTesting.csproj MauiTesting.csproj.bak
      sed -i'' -e 's/net7.0-ios;net7.0-maccatalyst/net7.0-ios/g' MauiTesting.csproj

      ../dotnet publish -bl:MauiiOS.binlog -f net7.0-ios --self-contained -r ios-arm64 -c Release /p:_RequireCodeSigning=false /p:ApplicationId=net.dot.mauitesting
      mv ./bin/Release/net7.0-ios/ios-arm64/publish/MauiTesting.ipa ./MauiiOSDefault.ipa

      cp MauiTesting.csproj.bak MauiTesting.csproj
    displayName: Build MAUI Default iOS
    workingDirectory: $(Build.SourcesDirectory)/MauiTesting

  - script: |
      chmod -R a+r .
      # remove net7.0-maccatalyst to work around https://github.com/dotnet/sdk/issues/21877
      cp Microsoft.NetConf2021.Maui.csproj Microsoft.NetConf2021.Maui.csproj.bak
      sed -i'' -e 's/net7.0-ios;net7.0-maccatalyst/net7.0-ios/g' Microsoft.NetConf2021.Maui.csproj

      ../../../dotnet build ../Web/Components/Podcast.Components.Maui.csproj
      ../../../dotnet publish Microsoft.NetConf2021.Maui.csproj -bl:MauiiOSPodcast.binlog -f net7.0-ios --self-contained -r ios-arm64 -c Release /p:_RequireCodeSigning=false /p:ApplicationId=net.dot.netconf2021.maui
      mv ./bin/Release/net7.0-ios/ios-arm64/publish/Microsoft.NetConf2021.Maui.ipa ./MauiiOSPodcast.ipa

      cp Microsoft.NetConf2021.Maui.csproj.bak Microsoft.NetConf2021.Maui.csproj
    displayName: Build MAUI Podcast iOS
    workingDirectory: $(Build.SourcesDirectory)/dotnet-podcasts/src/Mobile

  - script: |
      chmod -R a+r .
      ../dotnet publish -bl:MauiMacCatalyst.binlog -f net7.0-maccatalyst -c Release
      mv ./bin/Release/net7.0-maccatalyst/maccatalyst-x64/MauiTesting.app ./MauiMacCatalystDefault.app
    displayName: Build MAUI MacCatalyst
    workingDirectory: $(Build.SourcesDirectory)/MauiTesting

  - script: |
      ./dotnet new maui-blazor -n MauiBlazorTesting
      cd MauiBlazorTesting
      cp $(Build.SourcesDirectory)/src/tests/Common/maui/MauiScenario.props ./Directory.Build.props
      cp $(Build.SourcesDirectory)/src/tests/Common/maui/MauiScenario.targets ./Directory.Build.targets
      cp $(Build.SourcesDirectory)/NuGet.config ./NuGet.config

      echo -e "using Microsoft.AspNetCore.Components;
        #if ANDROID
          using Android.App;
        #endif

        namespace MauiBlazorTesting.Pages
        {
            public partial class Index
            {
                protected override void OnAfterRender(bool firstRender)
                {
                    if (firstRender)
                    {
                        #if ANDROID
                          var activity = MainActivity.Context as Activity;
                          activity.ReportFullyDrawn();
                        #else
                          System.Console.WriteLine(\"__MAUI_Blazor_WebView_OnAfterRender__\");
                        #endif
                    }
                }
            }
        }" > Pages/Index.razor.cs

      sed -i'' -e "s/{/{\npublic static Android.Content.Context Context { get; private set; }\npublic MainActivity() { Context = this; }/g" Platforms/Android/MainActivity.cs
    displayName: Setup MAUI Blazor Hybrid Project
    workingDirectory: $(Build.SourcesDirectory)

  - script: |
      chmod -R a+r .
      # remove net7.0-maccatalyst to work around https://github.com/dotnet/sdk/issues/21877
      cp MauiBlazorTesting.csproj MauiBlazorTesting.csproj.bak
      sed -i'' -e 's/net7.0-ios;net7.0-maccatalyst/net7.0-ios/g' MauiBlazorTesting.csproj

      # NuGet.config file cannot be in the build directory currently due to https://github.com/dotnet/aspnetcore/issues/41397
      rm NuGet.config

      ../dotnet publish -bl:MauiBlazoriOS.binlog -f net7.0-ios --self-contained -r ios-arm64 -c Release /p:_RequireCodeSigning=false /p:ApplicationId=net.dot.mauiblazortesting
      mv ./bin/Release/net7.0-ios/ios-arm64/publish/MauiBlazorTesting.ipa ./MauiBlazoriOSDefault.ipa

      # Restore NuGet.config
      cp $(Build.SourcesDirectory)/NuGet.config ./NuGet.config

      cp MauiBlazorTesting.csproj.bak MauiBlazorTesting.csproj
    displayName: Build MAUI Blazor iOS
    workingDirectory: $(Build.SourcesDirectory)/MauiBlazorTesting

  - script: |
      chmod -R a+r .

      # NuGet.config file cannot be in the build directory currently due to https://github.com/dotnet/aspnetcore/issues/41397
      rm NuGet.config

      ../dotnet publish -bl:MauiBlazorMacCatalyst.binlog -f net7.0-maccatalyst -c Release

      # Restore NuGet.config
      cp $(Build.SourcesDirectory)/NuGet.config ./NuGet.config

      mv ./bin/Release/net7.0-maccatalyst/maccatalyst-x64/MauiBlazorTesting.app ./MauiBlazorMacCatalystDefault.app
    displayName: Build MAUI Blazor MacCatalyst
    workingDirectory: $(Build.SourcesDirectory)/MauiBlazorTesting

  - task: PublishBuildArtifacts@1
    displayName: 'Publish MauiAndroid binlog'
    condition: always()
    inputs:
      pathtoPublish: $(Build.SourcesDirectory)/MauiTesting/MauiAndroid.binlog
      artifactName:  ${{ parameters.artifactName }}

  - task: PublishBuildArtifacts@1
    displayName: 'Publish MauiiOS binlog'
    condition: always()
    inputs:
      pathtoPublish: $(Build.SourcesDirectory)/MauiTesting/MauiiOS.binlog
      artifactName:  ${{ parameters.artifactName }}

  - task: PublishBuildArtifacts@1
    displayName: 'Publish MauiMacCatalyst binlog'
    condition: always()
    inputs:
      pathtoPublish: $(Build.SourcesDirectory)/MauiTesting/MauiMacCatalyst.binlog
      artifactName:  ${{ parameters.artifactName }}

  - task: PublishBuildArtifacts@1
    displayName: 'Publish MauiBlazoriOS binlog'
    condition: always()
    inputs:
      pathtoPublish: $(Build.SourcesDirectory)/MauiBlazorTesting/MauiBlazoriOS.binlog
      artifactName:  ${{ parameters.artifactName }}

  - task: PublishBuildArtifacts@1
    displayName: 'Publish MauiBlazorMacCatalyst binlog'
    condition: always()
    inputs:
      pathtoPublish: $(Build.SourcesDirectory)/MauiBlazorTesting/MauiBlazorMacCatalyst.binlog
      artifactName:  ${{ parameters.artifactName }}

  - task: PublishBuildArtifacts@1
    displayName: 'Publish MauiiOSPodcast binlog'
    condition: always()
    inputs:
      pathtoPublish: $(Build.SourcesDirectory)/dotnet-podcasts/src/Mobile/MauiiOSPodcast.binlog
      artifactName:  ${{ parameters.artifactName }}

  - template: /eng/pipelines/common/upload-artifact-step.yml
    parameters:
        rootFolder: $(Build.SourcesDirectory)/MauiTesting/MauiiOSDefault.ipa
        includeRootFolder: true
        displayName: Maui iOS IPA
        artifactName: MauiiOSDefaultIPA
        archiveExtension: '.tar.gz'
        archiveType: tar
        tarCompression: gz

  - template: /eng/pipelines/common/upload-artifact-step.yml
    parameters:
        rootFolder: $(Build.SourcesDirectory)/MauiBlazorTesting/MauiBlazoriOSDefault.ipa
        includeRootFolder: true
        displayName: Maui Blazor iOS IPA
        artifactName: MauiBlazoriOSDefaultIPA
        archiveExtension: '.tar.gz'
        archiveType: tar
        tarCompression: gz

  - template: /eng/pipelines/common/upload-artifact-step.yml
    parameters:
        rootFolder: $(Build.SourcesDirectory)/dotnet-podcasts/src/Mobile/MauiiOSPodcast.ipa
        includeRootFolder: true
        displayName: Maui iOS Podcast IPA
        artifactName: MauiiOSPodcastIPA
        archiveExtension: '.tar.gz'
        archiveType: tar
        tarCompression: gz

  - template: /eng/pipelines/common/upload-artifact-step.yml
    parameters:
        rootFolder: $(Build.SourcesDirectory)/MauiTesting/MauiMacCatalystDefault.app
        includeRootFolder: true
        displayName: Maui MacCatalyst App
        artifactName: MauiMacCatalystDefault
        archiveExtension: '.tar.gz'
        archiveType: tar
        tarCompression: gz

  - template: /eng/pipelines/common/upload-artifact-step.yml
    parameters:
        rootFolder: $(Build.SourcesDirectory)/MauiBlazorTesting/MauiBlazorMacCatalystDefault.app
        includeRootFolder: true
        displayName: Maui Blazor MacCatalyst App
        artifactName: MauiBlazorMacCatalystDefault
        archiveExtension: '.tar.gz'
        archiveType: tar
        tarCompression: gz

  - script: rm -r -f ./bin
    workingDirectory: $(Build.SourcesDirectory)/MauiTesting
    displayName: Clean MauiTesting bin directory
    condition: succeededOrFailed()

  - script: rm -r -f ./bin
    workingDirectory: $(Build.SourcesDirectory)/MauiBlazorTesting
    displayName: Clean MauiBlazorTesting bin directory
    condition: succeededOrFailed()

  - template: /eng/pipelines/common/upload-artifact-step.yml
    parameters:
      osGroup: ${{ parameters.osGroup }}
      osSubgroup: ${{ parameters.osSubgroup }}
      archType: ${{ parameters.archType }}
      buildConfig: ${{ parameters.buildConfig }}
      runtimeFlavor: ${{ parameters.runtimeFlavor }}
      helixQueues: ${{ parameters.helixQueues }}
      targetRid: ${{ parameters.targetRid }}
      nameSuffix: ${{ parameters.nameSuffix }}
      platform: ${{ parameters.platform }}
      shouldContinueOnError: ${{ parameters.shouldContinueOnError }}
      rootFolder:  ${{ parameters.rootFolder }}
      includeRootFolder:  ${{ parameters.includeRootFolder }}
      displayName:  ${{ parameters.displayName }}
      artifactName:  ${{ parameters.artifactName }}
      archiveExtension:  ${{ parameters.archiveExtension }}
      archiveType:  ${{ parameters.archiveType }}
      tarCompression:  ${{ parameters.tarCompression }}
